package org.oka.camsa.client;

import java.util.Collection;
import java.util.List;

import org.oka.camsa.model.Item;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.Padding;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;

public class CamsAppLayout extends LayoutContainer {

	private VerticalPanel vp;
	//private FormData formData;
	final Window window = new Window();
	private String logout;
	List<Item> listItem = null;
	HTML h = new HTML();
	String text = " ssss ";
	
	public CamsAppLayout(String user) {
		logout = user;
	}	
	
	protected void onRender(Element target, int index) {
		super.onRender(target, index);

		window.setHeading("Camsa beta");
		window.setSize(400, 200);
		window.setMinWidth(50);
		window.setFrame(true);

		final BorderLayout layout = new BorderLayout();
		setLayout(layout);
		setStyleAttribute("padding", "10px");

		
		ContentPanel north = new ContentPanel();
		north.add(new Anchor("logout", logout));
		north.setHeaderVisible(false);

		ContentPanel west = new ContentPanel();

		west.setHeading("CAMSA Menu");
		west.setBodyBorder(false);

		west.setLayout(new AccordionLayout());
		//west.setIcon(Resources.ICONS.accordion());

		ContentPanel cp = new ContentPanel();
		cp.setAnimCollapse(false);
		cp.setHeading("Item");

		VBoxLayout westLayout = new VBoxLayout();
		westLayout.setPadding(new Padding(5));
		westLayout.setVBoxLayoutAlign(VBoxLayoutAlign.STRETCH);
		
		final Button btn = new Button("DB Go!");
		btn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				ItemServiceAsync itemService = GWT.create(ItemService.class);
				itemService.dataStoreDump(new AsyncCallback<Void>() {
					public void onFailure(Throwable error) {}
					@Override
					public void onSuccess(Void arg0) {}
				});
			}
		});
		
		final Button btn2 = new Button("DB Search!");
		btn2.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				ItemServiceAsync itemService = GWT.create(ItemService.class);
				itemService.getAllItem(new AsyncCallback<Collection<Item>>() {

					@Override
					public void onFailure(Throwable arg0) {}

					@Override
					public void onSuccess(Collection<Item> arg0) {
						listItem = (List<Item>) arg0;		
//						
//						for (Item item : listItem) {
//							text = text + item.getCatalanName();
//							for (Price price : item.getPriceHistory()) {
//								text = text +  price.getDate().toString();
//							}
//						}
						window.setHeading("There are this number of items:: " + listItem.size());
						window.show();
					}
				});
			}
		});
		cp.add(btn);
		cp.add(btn2);
				
		cp.setLayout(westLayout);
		west.add(cp);

		cp = new ContentPanel();
		cp.setAnimCollapse(false);
		cp.setBodyStyleName("pad-text");
		cp.setHeading("Settings");
		cp.addText("aaaa");
		west.add(cp);

		cp = new ContentPanel();
		cp.setAnimCollapse(false);
		cp.setBodyStyleName("pad-text");
		cp.setHeading("Stuff");
		cp.addText("bbbb");
		west.add(cp);

		cp = new ContentPanel();
		cp.setAnimCollapse(false);
		cp.setBodyStyleName("pad-text");
		cp.setHeading("More Stuff");
		cp.addText("cccc");
		west.add(cp);
		west.setSize(200, 325);

		ContentPanel center = new ContentPanel();		
		center.setScrollMode(Scroll.AUTOX);
		center.setHeaderVisible(false);
		center.add(new HTML(text));

		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 100);
		northData.setMargins(new Margins(0, 0, 5, 0));

		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 150);
		westData.setMargins(new Margins(0, 5, 0, 0));

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(0));

		//formData = new FormData("-20");
		vp = new VerticalPanel();
		vp.setLayout(new CenterLayout());
		vp.setSpacing(10);
		// createForm2();

		// center.add(vp, new BorderLayoutData(LayoutRegion.CENTER));

		add(north, northData);
		add(west, westData);
		add(center, centerData);

	}
}